//Random Game Engine
//Copyright (C) 2013  Nikolay Dionisov

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "WindowsPlatform.h"
#include "..\..\Utilities\MemoryPool.h"

namespace re
{
	namespace core
	{
		WindowsPlatform* platform = NULL;
		WindowsPlatform::WindowsPlatform()
		{
			platform = this;
			registered = false;
		}
		WindowsPlatform::~WindowsPlatform()
		{
			platform = NULL;
		}
		LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
		{
			return platform->WndProc(hwnd, msg, wParam, lParam);
		}
		LRESULT WindowsPlatform::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
		{
			if(msg == WM_CLOSE)
			{
				man_container->environment_manager->ShutDownTheEngine();
			}
			return DefWindowProc(hwnd, msg, wParam, lParam);
		}
		IWindow* WindowsPlatform::VCreateWindow(std::wstring& title)
		{
			if(!registered)
			{
				WNDCLASSEX wc;
				//Register the Window Class
				wc.cbSize        = sizeof(WNDCLASSEX);
				wc.style         = 0;
				wc.lpfnWndProc   = re::core::WndProc;
				wc.cbClsExtra    = 0;
				wc.cbWndExtra    = 0;
				wc.hInstance     = GetModuleHandle(NULL);
				wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
				wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
				wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
				wc.lpszMenuName  = NULL;
				wc.lpszClassName = L"reapplication";
				wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

				if(!RegisterClassEx(&wc))
				{
					MessageBox(NULL, L"Window Registration Failed!", L"Error!",
						MB_ICONEXCLAMATION | MB_OK);
					return 0;
				}
				registered = true;
			}
			WindowsWindow* win = RE_NEW WindowsWindow;
			win->hwnd_ = CreateWindowEx(
				NULL,
				L"reapplication",
				title.c_str(),
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
				NULL, NULL, GetModuleHandle(NULL), NULL);

			ShowWindow(win->hwnd_, SW_SHOWDEFAULT);
			UpdateWindow(win->hwnd_);
			return win;
		}
		void WindowsPlatform::VDestroyWindow(IWindow* window)
		{
			
		}
		ISystem* WindowsPlatform::LoadModule(const std::wstring& source)
		{
			HMODULE module = LoadLibrary(source.c_str());
			if(module == NULL)
			{
				ASSERT(0);
				return NULL;
			}
			CreateSystemPtr proc = (CreateSystemPtr)GetProcAddress(module, "CreateSystem");
			if(proc == NULL)
			{
				ASSERT(0);
				return NULL;
			}
			return proc();
		}
		void WindowsPlatform::VProcessMessages()
		{
			MSG msg;
			while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
}